import datetime

from sqlalchemy import Column, String, Integer, DateTime,  ForeignKey
from sqlalchemy.orm import relationship

from database import db
from .member import Member
from .offer import Offer
from .product import Product
from ..models.additional_item import AdditionalItem
class ShopCart(db.Model):
    id = Column(Integer, primary_key=True, autoincrement=True)
    member_id = Column(ForeignKey(Member.id))
    created_at = Column(DateTime, default=datetime.datetime.now)
    updated_at = Column(DateTime,default=datetime.datetime.now)
    status = Column(String(20), default='created')

class CartItem(db.Model):
    STATUS_REMOVED = 'remove'
    STATUS_NORMAL = 'normal'
    STATUS_ORDERED = 'ordered'
    id = Column(Integer, primary_key=True, autoincrement=True)
    product_id = Column(ForeignKey(Product.id))
    offer_id = Column(ForeignKey(Offer.id))
    shop_cart_id = Column(ForeignKey(ShopCart.id))
    count = Column(Integer,default=0)
    created_at = Column(DateTime, default=datetime.datetime.now)
    status = Column(String(20), default='normal')
    source = Column(String(100))
    offer = relationship(Offer)
    product = relationship(Product)
    additives = relationship(AdditionalItem, secondary="cart_item_additive")

class CartItemAdditive(db.Model):
    '''
        购物车条目的附加项。
        可附加 ： service - 服务
    '''

    id = Column(Integer, primary_key=True, autoincrement=True)
    cart_item_id = Column(ForeignKey(CartItem.id))
    additional_id = Column(ForeignKey(AdditionalItem.id))

    cart_item = relationship(CartItem)
    additional_item = relationship(AdditionalItem)
    # title = Column(String(80))
    # description = Column(String(200),default=None)
    # type = Column(String(20))
    # required = Column(String(1), default='Y')
    # cart_item = Column(ForeignKey(CartItem.id))
